package q1161_maxLevelSum;

import CommonClass.Common.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    /*
    找那一层和最大
    层序遍历简单解决
     */
    public int maxLevelSum(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int ans = 1, maxVal = Integer.MIN_VALUE, count = 0;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int cur = 0;
            for (int i = 0; i < size; ++i) {
                TreeNode temp = queue.poll();
                cur += temp.val;
                if (temp.left != null) queue.offer(temp.left);
                if (temp.right != null) queue.offer(temp.right);
            }
            ++count;
            if (cur > maxVal) {
                ans = count;
                maxVal = cur;
            }

        }

        return ans;
    }
}
